<!--
 * @Description: 检测类型管理页面
-->
<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :page-no="71"
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :has-save="false"
        :has-cancel="false"
        @add="add"
        @cancel="cancel('formData')"
        @edit="edit"
        @del="del"
        @save="save('formData')"
      />
    </template>
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" @search="search"></SearchForm>
    </template>
    <template v-slot:table-box>
      <el-tree
        ref="tree"
        :props="treerule"
        :data="treeData"
        show-checkbox
        node-key="detectionTypeNo"
        default-expand-all
        :check-strictly="true"
        :accordion="true"
        @node-click="handleEdit"
        @check-change="handleSelectionChange"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog :title="dialogTitle + '类型信息'" :visible.sync="dialogFormVisible" :before-close="cancel">
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item
            label="检测类型名称:"
            prop="detectionTypeName"
            class="w100p"
          >
            <el-input
              v-model="formData.detectionTypeName"

              placeholder="请输入检测类型名称"
              clearable
            />
          </el-form-item>
          <el-form-item label="备注:" prop="remark" class="w100p">
            <el-input
              v-model="formData.remark"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4 }"

              clearable
            />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button class="zh-button-two" type="primary" @click="save('formData')">
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import SearchForm from '@/components/searchForm';
import ActionBar from "@/components/ActionBar";
import detectionTypeManage from "@/api/digital-operation/detection-manage-ment/detection-type-manage/index";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import { mymixin } from "@/utils/mixins";
export default {
  name: "DetectionTypeManage",
  components: { SearchForm, ActionBar, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      formConfig: [
        {
          label: '检测类型名称',
          prop: 'detectionTypeName',
          type: 'input',
        }
      ],
      searchlist: {},
      // 获取树状图子数据
      treerule: {
        children: "child",
        label: "detectionTypeName"
      },
      rules: {
        detectionTypeName: [{ required: true, message: "请输入检测类型名称", trigger: "blur" }]
      },
      // 获取树状图
      treeData: [],
      dialogTitle: "",
      dialogFormVisible: false
    };
  },
  watch: {},
  created() {
    // 获取树状图
    this.getTreeDataList();
  },
  mounted() {},
  methods: {
    // 获取数据
    getTreeDataList() {
      this.loading = true;
      if (this.searchlist.detectionTypeName === "") {
        this.formData = {};
        detectionTypeManage.getTreeTypeList().then(res => {
          this.loading = false;
          let data = res.data;
          if (data) {
            // 赋值数组
            this.treeData = data;
            this.resetActionBar();
          }
        });
        this.loading = true;
      } else {
        let param = {
          page: this.currentPage,
          size: this.pageSize,
          entity: {
            detectionTypeNo: this.detectionTypeNo,
            detectionTypeName: this.searchlist.detectionTypeName
          }
        };
        detectionTypeManage.postTreeTypeList(param).then(res => {
          this.loading = false;
          let data = res.data;
          if (data) {
            this.totalSize = data.totalElements;
            // 赋值数组
            this.treeData = data.content;
            this.resetActionBar();
          }
        });
      }
    },
    search() {
      this.currentPage = 1;
      this.getTreeDataList();
    },
    // 初始化操作栏状态
    handleEdit(row) {
      // TODO 点击行元素后 edit
      this.formData = JSON.parse(JSON.stringify(row));
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = true;
      this.canInput = false;
      // this.$refs.formData.resetFields();
    },
    // add(){
    //   this.formData = {};
    //   this.openDialog('add');
    // },
    // edit(){
    //   this.openDialog('edit');
    // },
    handleSelectionChange() {
      let list = this.$refs.tree.getCheckedKeys();
      this.detectionTypeNoList = [];
      this.detectionTypeNoList = list;
      if (this.detectionTypeNoList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      detectionTypeManage
        .allmetInfo(
          { detectionTypeNoList: this.detectionTypeNoList },
          "delList"
        )
        .then(res => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.getTreeDataList();
            this.resetActionBar();
          } else {
            this.$message.warning(res.msg || '删除失败');
          }
        });
    },
    save(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            let nowdataone = {
              remark: this.formData.remark,
              parentId: this.formData.parentId,
              detectionTypeName: this.formData.detectionTypeName,
              detectionTypeNo: this.formData.detectionTypeNo
            };
            detectionTypeManage.allmetInfo(nowdataone, "edit").then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "修改成功！");
                this.$refs[formData].resetFields()
                // this.closeDialog();
                this.getTreeDataList();
              } else {
                this.$message.error(res.msg || "修改失败！");
              }
            });
          } else if (this.nowChanceStau === "add") {
            let nowdata = {
              organizationNo: sessionStorage.organizationNo,
              remark: this.formData.remark,
              detectionTypeName: this.formData.detectionTypeName
            };
            if (this.formData.detectionTypeNo) {
              nowdata.parentId = this.formData.detectionTypeNo;
              // console.log("3" + nowdata.parentId);
              // console.log(this.nowdata);
            } else {
              nowdata.parentId = 0;
            }
            // 新增接口
            detectionTypeManage.allmetInfo(nowdata, "add").then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "新增成功！");
                this.$refs[formData].resetFields()
                // this.closeDialog();
                this.getTreeDataList();
              } else {
                this.$message.error(res.msg || "新增失败！");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === 'add' ? '新增' : '编辑'
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = '';
    }
  }
};
</script>

<style scoped lang="scss">
::v-deep .el-form-item__label {
 min-width: 120px !important;
}
</style>
